package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public PageResult<Activity> list(Integer channel, Integer type, Integer status, Integer page, Integer pageSize) {
        //设置分页查询
        PageHelper.startPage(page, pageSize);
        //2.调用mapper接口方法 执行查询
        List<Activity> list = activityMapper.selectlist(channel, type, status);
        //3.将查询结果封装成PageResult对象并返回
        Page<Activity> p = (Page<Activity>)list;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(Integer id) {
        activityMapper.deleteUsersById(id);
    }

    @Override
    public void add(Activity activity) {
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(activity.getCreateTime());
        activityMapper.insert(activity);
    }

    @Override
    public Activity getById(Integer id) {
        return activityMapper.getById(id);
    }

    @Override
    public void update(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public List<Activity> listByType(Integer type) {
        return activityMapper.listByType(type);
    }

    //获取所有活动

    @Override
    public List<Activity> list() {
        return activityMapper.list();

    }
}
